﻿using EventBus;
using LiteStep.Currencies;
using LiteStep.DataSources;
using LiteStep.Events;
using LiteStep.Events.Handlers;
using LiteStep.Exchanges;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteStep.Conversions.ConversionUpdaters
{
    public abstract class BasicDepthConversionUpdater<TFrom, TTo, TOn, TDepth> :
        IConversionUpdater<TFrom, TTo, TOn>,
        IAfterDepthResponseEventHandler<TDepth>
        where TFrom : ICurrency
        where TTo : ICurrency
        where TOn : IExchange
        where TDepth : IDepth
    {
        protected IConversionManager conversionManager { get; set; }
        protected IEventBus eventBus { get; set; }
        private IHandlerRegistration registration { get; set; }

        public BasicDepthConversionUpdater(IConversionManager conversionManager, IEventBus eventBus)
        {
            this.conversionManager = conversionManager;
            this.eventBus = eventBus;
        }

        public void Register()
        {
            if (registration == null)
            {
                registration = eventBus.AddHandler(AfterDepthResponseEvent<TDepth>.TYPE, this);
            }
        }

        public void Unregister()
        {
            if (registration != null)
            {
                registration.RemoveHandler();
                registration = null;
            }
        }

        public abstract void OnAfterDepthResponseEvent(AfterDepthResponseEvent<TDepth> e);
    }
}
